package com.forsir.android.mhfinance.storeObjects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;

import com.forsir.android.mhfinance.helper.DbAdapter;

public class CategoryList {
	private static List<Category> mCategories = new ArrayList<Category>();
	public static Category createNew = null;

	public static Category getCreateNewCategory() {
		if (createNew == null) {
			createNew = new Category();
			createNew.setId(-1);
			createNew.setName("Create new");
		}
		return createNew;
	}

	public static void setCategories(List<Category> categories) {
		mCategories = categories;
	}

	public static List<Category> getCategories() {
		return mCategories;
	}

	public static void loadAll(DbAdapter dbHelper) {
		final Cursor cursor = dbHelper.fetchAllEntries(new Category(), DbAdapter.CATEGORY_PARENT + " is null", DbAdapter.CATEGORY_NAME);
		mCategories = new ArrayList<Category>();

		if (!cursor.moveToFirst()) {
			cursor.close();
			return;
		}

		do {
			final Category category = new Category(cursor);
			category.loadSubcategories(dbHelper);
			mCategories.add(category);
		} while (cursor.moveToNext());
		cursor.close();
	}

	public static boolean isEmpty() {
		return mCategories.size() == 0;
	}

	public static String[] getNames() {
		final int size = mCategories.size();
		final String[] retString = new String[size];
		for (int i = 0; i < size; i++) {
			retString[i] = mCategories.get(i).getName();
		}
		return retString;
	}

	public static List<Map<String, String>> getMainGroup(Category addedCategory, boolean onlyIfEmpty) {
		final List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		final int count = mCategories.size();

		for (int i = 0; i < count; i++) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			curGroupMap.put("id", String.valueOf(mCategories.get(i).getId()));
			curGroupMap.put("name", mCategories.get(i).getName());
			groupData.add(curGroupMap);
		}

		// show when is empty or !onlyIfEmpty
		if (addedCategory != null && (count == 0 || !onlyIfEmpty)) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			curGroupMap.put("id", "-1");
			curGroupMap.put("name", addedCategory.getName());
			groupData.add(curGroupMap);
		}

		return groupData;
	}

	public static List<Map<String, String>> getMainGroup(String currencyBefore, String currencyAfter) {
		final List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		final int count = mCategories.size();
		Category category;
		float deposit;
		float withdraw;

		for (int i = 0; i < count; i++) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			category = mCategories.get(i);
			deposit = category.getSumDeposit();
			withdraw = category.getSumWithdraw();
			curGroupMap.put("id", String.valueOf(category.getId()));
			curGroupMap.put("name", category.getName());
			if (category.getSumCount() == 0) {
				curGroupMap.put("count", "");
			} else {
				curGroupMap.put("count", String.valueOf(category.getSumCount()) + "x");
			}
			curGroupMap.put("deposit", Payment.formatAmountString(currencyBefore, deposit, currencyAfter));
			curGroupMap.put("withdraw", Payment.formatAmountString(currencyBefore, withdraw, currencyAfter));
			curGroupMap.put("summary", Payment.formatAmountString(currencyBefore, withdraw + deposit, currencyAfter));
			groupData.add(curGroupMap);
		}

		return groupData;
	}

	public static List<List<Map<String, String>>> getChildGroup(Subcategory addedSubcategory, boolean onlyIfEmpty) {
		final List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

		final int countMain = mCategories.size();
		for (int i = 0; i < countMain; i++) {
			final Category actualCategory = mCategories.get(i);
			childData.add(actualCategory.getSubcategoryList().getMainGroup(addedSubcategory, onlyIfEmpty));
		}
		childData.add(new ArrayList<Map<String, String>>());
		return childData;
	}

	public static List<List<Map<String, String>>> getChildGroup(String currencyBefore, String currencyAfter) {
		final List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

		final int countMain = mCategories.size();
		for (int i = 0; i < countMain; i++) {
			final Category actualCategory = mCategories.get(i);
			childData.add(actualCategory.getSubcategoryList().getChildGroup(currencyBefore, currencyAfter));
		}
		childData.add(new ArrayList<Map<String, String>>());
		return childData;
	}

	public static void fillChildSummary(HashMap<Long, String[]> data) {
		// data must be sorted: id, count, deposit, withdraw
		final int count = mCategories.size();
		for (int i = 0; i < count; i++) {
			mCategories.get(i).getSubcategoryList().fillChildSummary(data);
		}
	}

	public static Category getCategory(int mainPosition) {
		if (mainPosition >= mCategories.size()) {
			return null;
		}
		return mCategories.get(mainPosition);
	}

	public static Subcategory getSubcategory(int mainPosition, int childPosition) {
		if (mainPosition >= mCategories.size()) {
			return null;
		}
		return mCategories.get(mainPosition).getSubcategoryList().getSubcategory(childPosition);
	}

	public static boolean isAddNew(int mainPosition, int childPosition) {
		if (mainPosition >= mCategories.size()) {
			return true;
		}
		return childPosition >= mCategories.get(mainPosition).getSubcategoryList().getSubcategories().size();
	}

	public static Category getCategoryById(long categoryId) {
		final int count = mCategories.size();
		for (int i = 0; i < count; i++) {
			if (mCategories.get(i).getId() == categoryId) {
				return mCategories.get(i);
			}
		}

		return null;
	}

	public static Subcategory getSubcategoryById(long subcategoryId) {
		final int count = mCategories.size();
		for (int i = 0; i < count; i++) {
			final Subcategory subcategory = mCategories.get(i).getSubcategoryList().getSubcategoryById(subcategoryId);

			if (subcategory != null) {
				return subcategory;
			}
		}

		return null;
	}

	public static int getSubcategoryParentPosById(long subcategoryId) {
		final int count = mCategories.size();
		for (int i = 0; i < count; i++) {
			final Subcategory subcategory = mCategories.get(i).getSubcategoryList().getSubcategoryById(subcategoryId);

			if (subcategory != null) {
				return i;
			}
		}

		return -1;
	}

	public static boolean hasSubcategories() {
		final int count = mCategories.size();
		int size = 0;
		for (int i = 0; i < count; i++) {
			size += mCategories.get(i).getSubcategoryList().getSubcategories().size();

			if (size > 0) {
				return true;
			}
		}

		return false;
	}

	public static void cleanSummary() {
		final int count = mCategories.size();
		for (int i = 0; i < count; i++) {
			mCategories.get(i).getSubcategoryList().cleanSummary();
		}
	}

	public static float getSumDeposit() {
		final int count = mCategories.size();
		float deposit = 0;

		for (int i = 0; i < count; i++) {
			deposit += mCategories.get(i).getSumDeposit();
		}
		return deposit;
	}

	public static float getSumWithdraw() {
		final int count = mCategories.size();
		float withdraw = 0;

		for (int i = 0; i < count; i++) {
			withdraw += mCategories.get(i).getSumWithdraw();
		}
		return withdraw;
	}

}
